package com.chen.common.scheduler.controller;

import com.chen.common.scheduler.config.JobTaskConfig;
import com.chen.common.scheduler.entity.JobTask;
import com.chen.root.base.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ~龙~
 */
@RestController
@RequestMapping("/job")
@Api(value = "job", tags = "单机版定时任务动态配置API")
public class JobController {

    @Resource
    private JobTaskConfig jobTaskConfig;

    @GetMapping("/refresh")
    @ApiOperation(value = "刷新定时任务配置", notes = "刷新定时任务配置，返回当前所有定时任务配置")
    public R<List<JobTask>> refresh() {
        jobTaskConfig.refresh();
        return R.ok(jobTaskConfig.getAllTaskConfig());
    }

    @PostMapping("/once")
    @ApiOperation(value = "执行一次", notes = "根据执行器code，执行一次定时任务")
    public R<Boolean> once(String code) throws Exception {
        jobTaskConfig.executeOnce(code);
        return R.ok(true);
    }
}
